interface HttpClientOptions {
    baseURL: string;
    timeout: number;
    headers: Record<string, string>;
    retries: number;
    requestInterceptors: Array<(config: RequestOptions) => RequestOptions>;  // 请求拦截器
    responseInterceptors: Array<(response: any) => any>;  // 响应拦截器
  }
  
  interface RequestOptions extends RequestInit {
    body?: string | null;
  }
  
  class HttpClient {
    // 默认配置
     static defaultOptions: HttpClientOptions = {
      baseURL: '',
      timeout: 10000,  // 请求超时 10 秒
      headers: {
        'Content-Type': 'application/json',
      },
      retries: 3,
      requestInterceptors: [],
      responseInterceptors: [],
    };
  
    // 添加请求拦截器
    static addRequestInterceptor(interceptor: (config: RequestOptions) => RequestOptions): void {
      this.defaultOptions.requestInterceptors.push(interceptor);
    }
  
    // 添加响应拦截器
    static addResponseInterceptor(interceptor: (response: any) => any): void {
      this.defaultOptions.responseInterceptors.push(interceptor);
    }
  
     /**
     * 发送 GET 请求
     * @param {string} url 请求的 URL
     * @param {Record<string, string>} params 查询参数
     * @param {Record<string, string>} headers 请求头
     * @returns {Promise<any>} 返回 Promise 对象
     */
     static get(url: string, params: Record<string, string> = {}, headers: Record<string, string> = {}): Promise<any> {
        // 拼接查询参数
        const queryString = new URLSearchParams(params).toString();
        const fullUrl = queryString ? `${url}?${queryString}` : url;

        return this.request(fullUrl, 'GET', null, headers);
    }
  
    /**
     * 发送 POST 请求
     * @param {string} url 请求的 URL
     * @param {any} data 请求体数据
     * @param {Record<string, string>} headers 请求头
     * @returns {Promise<any>} 返回 Promise 对象
     */
    static post(url: string, data: any, headers: Record<string, string> = {}): Promise<any> {
      return this.request(url, 'POST', data, headers);
    }
  
    /**
     * 发送 PUT 请求
     * @param {string} url 请求的 URL
     * @param {any} data 请求体数据
     * @param {Record<string, string>} headers 请求头
     * @returns {Promise<any>} 返回 Promise 对象
     */
    static put(url: string, data: any, headers: Record<string, string> = {}): Promise<any> {
      return this.request(url, 'PUT', data, headers);
    }
  
    /**
     * 发送 DELETE 请求
     * @param {string} url 请求的 URL
     * @param {Record<string, string>} headers 请求头
     * @returns {Promise<any>} 返回 Promise 对象
     */
    static delete(url: string, headers: Record<string, string> = {}): Promise<any> {
      return this.request(url, 'DELETE', null, headers);
    }
  
    /**
     * 发送请求
     * @param {string} url 请求的 URL
     * @param {string} method 请求方法
     * @param {any | null} data 请求体数据
     * @param {Record<string, string>} headers 请求头
     * @returns {Promise<any>} 返回 Promise 对象
     */
    private static request(url: string, method: string, data: any | null = null, headers: Record<string, string> = {}): Promise<any> {
      // 合并配置
      const options: RequestOptions = {
        method: method,
        headers: { ...this.defaultOptions.headers, ...headers },
        body: data ? JSON.stringify(data) : null, // POST 和 PUT 请求时将数据转换为 JSON 字符串
      };
  
      // 请求的完整 URL（如果有 baseURL）
      const fullUrl = this.defaultOptions.baseURL + url;
  
      // 执行请求拦截器
      const interceptedConfig = this._applyRequestInterceptors(options);
  
      // 返回一个 Promise，执行请求
      return new Promise((resolve, reject) => {
        // 进行请求重试
        this._makeRequestWithRetry(fullUrl, interceptedConfig, resolve, reject, 0);
      });
    }
  
    /**
     * 执行请求拦截器
     * @param {RequestOptions} config 请求配置
     * @returns {RequestOptions} 拦截后的请求配置
     */
    private static _applyRequestInterceptors(config: RequestOptions): RequestOptions {
      let interceptedConfig = { ...config };
      for (const interceptor of this.defaultOptions.requestInterceptors) {
        interceptedConfig = interceptor(interceptedConfig);  // 依次执行请求拦截器
      }
      return interceptedConfig;
    }
  
    /**
     * 执行响应拦截器
     * @param {any} response 响应数据
     * @returns {any} 拦截后的响应数据
     */
    private static _applyResponseInterceptors(response: any): any {
      let interceptedResponse = response;
      for (const interceptor of this.defaultOptions.responseInterceptors) {
        interceptedResponse = interceptor(interceptedResponse);  // 依次执行响应拦截器
      }
      return interceptedResponse;
    }
  
    /**
     * 发起请求并处理重试
     * @param {string} url 请求的 URL
     * @param {RequestOptions} options 请求选项
     * @param {Function} resolve 成功回调
     * @param {Function} reject 失败回调
     * @param {number} attempt 当前尝试次数
     */
    private static _makeRequestWithRetry(url: string, options: RequestOptions, resolve: (value: any) => void, reject: (reason?: any) => void, attempt: number): void {
      // 超时控制
      const controller = new AbortController();
      const timeoutId = setTimeout(() => controller.abort(), this.defaultOptions.timeout);
  
      fetch(url, { ...options, signal: controller.signal })
        .then(response => {
          clearTimeout(timeoutId);  // 清除超时
          if (!response.ok) {
            throw new Error(`HTTP error! Status: ${response.status}`);
          }
          return response.json();
        })
        .then(data => {
          // 执行响应拦截器
          const interceptedResponse = this._applyResponseInterceptors(data);
          resolve(interceptedResponse);
        })
        .catch(error => {
          // 处理请求错误，包括超时和 HTTP 错误
          clearTimeout(timeoutId);  // 清除超时
          if (attempt < this.defaultOptions.retries) {
            // 如果重试次数没有达到限制，则重试请求
            console.log(`Retrying request... Attempt ${attempt + 1}`);
            this._makeRequestWithRetry(url, options, resolve, reject, attempt + 1);
          } else {
            reject(error);  // 超过最大重试次数，抛出错误
          }
        });
    }
  
    // 以下是设置默认配置的方法...
  
  }
  
  export default HttpClient;


